/*
 * FileTransferHandler.java
 *
 * Created on December 10, 2006, 9:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package eu.somatik.somaftp.gui.dnd;

import eu.somatik.somaftp.logic.FileInterface;
import eu.somatik.somaftp.logic.FileSystemInterface;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.TransferHandler;

/**
 * TODO implement drag overlay images with alpha
 * TODO only copy to other filesystem
 * TODO make use of line showing see blogs
 *
 * @author francisdb
 *
 *
 */
public class FileTransferHandler extends TransferHandler{
    
    
    protected Transferable createTransferable(JComponent c) {
        Transferable selection = null;
        if (c instanceof JTable){
            JTable table = (JTable) c;
            int[] rows = table.getSelectedRows();
            FileInterface[] files = new FileInterface[rows.length];
            for(int i=0;i<files.length;i++){
                files[i] = (FileInterface)table.getValueAt(rows[i],0);
            }
            selection = new FileSelection(files);
        }
        
        return selection;
    }
    
    public int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }
    
    public boolean importData(JComponent c, Transferable t) {
        if (canImport(c, t.getTransferDataFlavors())) {
            try {
                FileSelection file = (FileSelection)t.getTransferData(DataFlavor.stringFlavor);
                System.out.println(file.getTransferData(FileSelection.FLAVOR));
                return true;
            } catch (UnsupportedFlavorException ufe) {
                ufe.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        
        return false;
    }
    
    
    
    protected void exportDone(JComponent c, Transferable data, int action) {
        // todo
    }    
    
    public boolean canImport(JComponent c, DataFlavor[] flavors) {
        for (int i = 0; i < flavors.length; i++) {
            if (FileSelection.FLAVOR.equals(flavors[i])) {
                return true;
            }
        }
        return false;
    }
    
    public boolean canImport(TransferSupport supp) {
        /* for the demo, we'll only support drops (not clipboard paste) */
        if (!supp.isDrop()) {
            return false;
        }
        
        /* return false if the drop doesn't contain a list of files */
        if (!supp.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            return false;
        }
        
    /* check to see if the source actions (a bitwise-OR of supported
     * actions) contains the COPY action
     */
        boolean copySupported = (COPY & supp.getSourceDropActions()) == COPY;
        
        /* if COPY is supported, choose COPY and accept the transfer */
        if (copySupported) {
            supp.setDropAction(COPY);
            return true;
        }
        
    /* COPY isn't supported, so reject the transfer.
     *
     * Note: If you want to accept the transfer with the default
     *       action anyway, you could instead return true.
     */
        return false;
    }
    
}
